/*
 *  This file is part of Bracket Properties
 *  Copyright 2011-2013 David R. Smith
 *  All Rights Reserved
 *
 */
package asia.redact.bracket.properties.serializer;

/**
 * In the serializer we take an accessor in the java binding of the form (get|is)Name and turn that
 * into a property which has a terminal like .name. This is the 'custom key' part of this class, 
 * the key generated from getName into name. The 'object' is the object returned from the accessor call
 * 
 * @author Dave
 *
 */
public class Token {

	public static final Token NULL_WRAPPER = new Token(0,TokenType.N, null, null);
	
	private final String key;
	private final Object object;
	private final TokenType tokenType;
	private final long id;
	// set if we are dealing with an ARRAY token type
	private long arrayLength;

	public Token(long id, TokenType type, String key, Object object) {
		super();
		this.id = id;
		tokenType = type;
		this.key = key;
		this.object = object;
	}

	public String getKey() {
		return key;
	}

	public Object getObject() {
		return object;
	}
	
	public String getPackageName(){
		if(object == null) return null;
		return object.getClass().getPackage().getName();
	}

	public TokenType getTokenType() {
		return tokenType;
	}

	@Override
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("Token [");
		buf.append(id);
		buf.append(", ");
		buf.append(tokenType);
		buf.append(", ");
		buf.append(key);
		buf.append(", ");
		buf.append(object);
		buf.append("]");
		return buf.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (arrayLength ^ (arrayLength >>> 32));
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		result = prime * result + ((object == null) ? 0 : object.hashCode());
		result = prime * result
				+ ((tokenType == null) ? 0 : tokenType.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Token other = (Token) obj;
		if (arrayLength != other.arrayLength)
			return false;
		if (id != other.id)
			return false;
		if (key == null) {
			if (other.key != null)
				return false;
		} else if (!key.equals(other.key))
			return false;
		if (object == null) {
			if (other.object != null)
				return false;
		} else if (!object.equals(other.object))
			return false;
		if (tokenType != other.tokenType)
			return false;
		return true;
	}

	public long getId() {
		return id;
	}
	
	public boolean isRenderImmediate() {
		return tokenType.isRenderImmediate();
	}

	public long getArrayLength() {
		return arrayLength;
	}

	public void setArrayLength(long arrayLength) {
		this.arrayLength = arrayLength;
	}
	
}
